package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.List;

import com.ruoyi.system.domain.SocietyManage;
import com.ruoyi.system.domain.SysFinanceLog;
import com.ruoyi.system.mapper.SocietyManageMapper;
import com.ruoyi.system.mapper.SysFinanceLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserSocietyMapper;
import com.ruoyi.system.domain.SysUserSociety;
import com.ruoyi.system.service.ISysUserSocietyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 社团用户关联Service业务层处理
 *
 * @author 新一
 * @date 2021-12-22
 */
@Service
public class SysUserSocietyServiceImpl implements ISysUserSocietyService {
    @Autowired
    private SysUserSocietyMapper sysUserSocietyMapper;
    @Autowired
    private SocietyManageMapper societyManageMapper;
    @Autowired
    private SysFinanceLogMapper sysFinanceLogMapper;

    /**
     * 查询社团用户关联
     *
     * @param id 社团用户关联主键
     * @return 社团用户关联
     */
    @Override
    public SysUserSociety selectSysUserSocietyById(Long id) {
        return sysUserSocietyMapper.selectSysUserSocietyById(id);
    }

    /**
     * 查询社团用户关联列表
     *
     * @param sysUserSociety 社团用户关联
     * @return 社团用户关联
     */
    @Override
    public List<SysUserSociety> selectSysUserSocietyList(SysUserSociety sysUserSociety) {
        return sysUserSocietyMapper.selectSysUserSocietyList(sysUserSociety);
    }

    /**
     * 新增社团用户关联
     *
     * @param sysUserSociety 社团用户关联
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserSociety(SysUserSociety sysUserSociety) {
        sysUserSociety.setSocietyId(sysUserSociety.getId());
        sysUserSociety.setId(null);
        sysUserSociety.setUserId(SecurityUtils.getUserId());
        sysUserSociety.setUserName(SecurityUtils.getUsername());
        try {
            SocietyManage society = societyManageMapper.selectSocietyManageById(sysUserSociety.getSocietyId());

            int countSave = sysUserSocietyMapper.insertSysUserSociety(sysUserSociety);
            if (countSave != 0) {
                SocietyManage societyManage = new SocietyManage();
                societyManage.setId(sysUserSociety.getSocietyId());
                int count = societyManageMapper.updateSocietyAmount(societyManage);
                if (count == 0) {
                    throw new ServiceException("加入社团失败");
                }
            }
            SysFinanceLog sysFinanceLog = new SysFinanceLog();
            sysFinanceLog.setSocietyId(society.getId() + "");
            sysFinanceLog.setSocietyName(society.getSocietyName() + "");
            sysFinanceLog.setFinanceDetail("+" + society.getCharge());
            sysFinanceLog.setFinanceRemark("社团入会");
            sysFinanceLog.setOperator(SecurityUtils.getUsername());
            sysFinanceLogMapper.insertSysFinanceLog(sysFinanceLog);
            return countSave;
        } catch (Exception e) {
            throw new ServiceException("您已加入该社团");
        }
    }

    /**
     * 修改社团用户关联
     *
     * @param sysUserSociety 社团用户关联
     * @return 结果
     */
    @Override
    public int updateSysUserSociety(SysUserSociety sysUserSociety) {
        return sysUserSocietyMapper.updateSysUserSociety(sysUserSociety);
    }

    /**
     * 批量删除社团用户关联
     *
     * @param ids 需要删除的社团用户关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserSocietyByIds(Long[] ids) {
        return sysUserSocietyMapper.deleteSysUserSocietyByIds(ids, SecurityUtils.getUserId());
    }

    /**
     * 删除社团用户关联信息
     *
     * @param id 社团用户关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserSocietyById(Long id) {
        return sysUserSocietyMapper.deleteSysUserSocietyById(id);
    }
}
